package com.edu.edu.day25_homework.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.edu.edu.day25_homework.R;
import com.edu.edu.day25_homework.bean.Movies;
import com.edu.edu.day25_homework.cache.BitmapCache;
import com.edu.edu.day25_homework.thread.loadImageTask;

import java.util.List;

/**
 * Created by Administrator on 2016/9/10.
 */
public class MoviesAdapter extends BaseAdapter{

    private List<Movies> mData;
    private LayoutInflater inflater;

    public void setData(List<Movies> data){
        this.mData = data;
        notifyDataSetChanged();
    }

    public  MoviesAdapter(Context context){
        inflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return mData!=null ? mData.size():0;
    }

    @Override
    public Object getItem(int position) {
        return mData!=null ? mData.get(position):null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertview, ViewGroup parent) {
        final ViewHolder holder;
        if(convertview == null){
            convertview = inflater.inflate(R.layout.movie_list_item,parent,false);
//            convertview=LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_list_item,parent,false);
            holder = new ViewHolder(convertview);
            convertview.setTag(holder);

        }else{
            holder = (ViewHolder) convertview.getTag();
        }

        Movies movies = mData.get(position);

        holder.movieName.setText(movies.getName());
        holder.movieGrade.setText(movies.getGrade());
        holder.movieHighlight.setText(movies.getHighlight());
        holder.movieActors.setText(movies.getActors());
        final String logo = movies.getLogo();

        Bitmap bitmap = BitmapCache.getInstance().get(logo);
        //如果缓存中没有数据,则开启异步任务加载图片
        if(bitmap == null){
            new loadImageTask(holder.movieLogo).execute(logo);
        }else{
            //如果缓存中有图片，直接利用
            holder.movieLogo.setImageBitmap(bitmap);
        }
//        Bitmap bitmap = BitmapCache.getInstance().get(logo);
////        new loadImageTask(holder.movieLogo).execute(logo);
//        if(bitmap == null){
//            new loadImageTask(new loadImageTask.onGetImageListener() {
//                @Override
//                public void onGetImage(Bitmap result) {
//                    holder.movieLogo.setImageBitmap(result);
//                }
//            }).execute(logo);
//        }else{
//            holder.movieLogo.setImageBitmap(bitmap);
//        }


        return convertview;
    }
    class ViewHolder{
        ImageView movieLogo;
        TextView movieName,movieGrade,movieHighlight,movieActors;
        public ViewHolder(View convertview){
            movieLogo = (ImageView) convertview.findViewById(R.id.movie_image);
            movieName = (TextView) convertview.findViewById(R.id.movie_name);
            movieGrade = (TextView) convertview.findViewById(R.id.movie_grade);
            movieHighlight = (TextView) convertview.findViewById(R.id.movie_highlight);
            movieActors = (TextView) convertview.findViewById(R.id.movie_actors);
        }
    }
}
